<template>
	<el-dialog
	  :title="!dataForm.workshopId ? '新增职位' : '修改职位'"
	  :visible.sync="visible"
	  :before-close="close"
	  :close-on-click-modal="false"
	  width="50%">
	  <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-position="top">
	  	<el-form-item prop="positionName" label="职位名称">
        <el-input v-model="dataForm.positionName" placeholder="职位名称"></el-input>
     	</el-form-item>
     	<el-form-item prop="departmentId" label="所属部门">
        <el-select v-model="dataForm.departmentId" filterable v-if="departments.length" placeholder="所属部门">
          <el-option
            v-for="item in departments"
            :key="item.departmentId"
            :label="item.departmentName"
            :value="item.departmentId">
          </el-option>
        </el-select>
      </el-form-item>
	  </el-form>
	  <span slot="footer" class="dialog-footer">
	    <el-button @click="close">取 消</el-button>
	    <el-button type="primary" class="pan-btn primary-btn" @click="dataFormSubmit('dataForm')">确 定</el-button>
  	</span>
	</el-dialog>
</template>

<script>
	import { saveOrUpdatePosition } from '@/api/position'
	import { getDepartmentList } from '@/api/department'
	import {CODE_OK} from '@/common/js/config'
	export default {
		data() {
			return {
				departments: [],
				visible: false,
				dataForm: {
					positionName: "",
					departmentName: ""
				},
				dataRule: {
					positionName: [
          	{ required: true, message: '车间名称不能为空', trigger: 'blur' }
        	],
        	departmentId: [
          	{ required: true, message: '所属部门不能为空', trigger: 'blur' }
        	]
				}
			}
		},
		props: ["childDataForm"],
		created() {
			if (!this.childDataForm == null || !this.childDataForm == "" || this.childDataForm || !this.childDataForm == undefined) {
				this.dataForm = this.childDataForm
			}
			this.visible = true
			this.getDepartmentList()
		},
		methods: {
			getDepartmentList() {
				const params = {
					page: 1,
    			limit: 9999999
				}
	  		getDepartmentList(params).then(res => {
	  			if (res.code === CODE_OK) {
	  				this.departments = res.page.list
	  			}else{
	  				this.$message.error(res.msg)
	  			}
	  		}).catch()
			},
			close() {
				this.$emit('close')
				this.$emit('refreshData')
				this.visible = false
			},
			dataFormSubmit(form) {
				this.$refs[form].validate(valid => {
					if (valid) {
						saveOrUpdatePosition(this.dataForm).then(res => {
							if (res.code === CODE_OK) {
								this.$message({
	                message: '操作成功',
	                type: 'success',
	                duration: 1500,
	                onClose: this.close()
              	})
							}else{
								this.$message.error(res.msg)
							}
						})
					}else{
						this.$message.error("请按要求填写")
					}
				})
			}
    }
	}
</script>

<style>
</style>